package com.example.apartment.controller;

import com.example.apartment.common.ApiRestResponse;
import com.example.apartment.common.RespExceptionEnum;
import com.example.apartment.domain.Admin;
import com.example.apartment.domain.Building;
import com.example.apartment.service.BuildingService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 宿舍控制类
 *
 * @author wuzexin
 * @date 2021/11/2 14:49
 */
@RestController
@RequestMapping("/admin/building")
public class BuildingController {

    @Resource
    private BuildingService buildingService;

    /*
        创建宿舍楼
     */
    @PostMapping("/create")
    public ApiRestResponse create(String buildingName, Integer floorCount, Integer roomCount, HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        // 创建宿舍楼
        Building building = buildingService.create(admin, buildingName, floorCount, roomCount);

        // 返回宿舍楼信息
        return ApiRestResponse.success(building);
    }

    /*
        修改宿舍楼信息
     */
    @PostMapping("/update")
    public ApiRestResponse update(Integer id, String buildingName, Integer floorCount, Integer roomCount, HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        // 修改宿舍楼信息
        Building building = buildingService.update(admin, id, buildingName, floorCount, roomCount);

        // 返回宿舍楼信息
        return ApiRestResponse.success(building);
    }

    /*
        删除宿舍楼
     */
    @PostMapping("/delete")
    public ApiRestResponse delete(Integer id, HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        // 删除宿舍楼
        buildingService.delete(admin, id);

        return ApiRestResponse.success();
    }

    /*
        查询所有宿舍楼信息
     */
    @PostMapping("/select")
    public ApiRestResponse select(HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        List<Building> list = buildingService.select(admin);

        return ApiRestResponse.success(list);
    }

    /*
        根据id查询宿舍楼信息
     */
    @PostMapping("/selectById")
    public ApiRestResponse selectById(Integer id, HttpSession session) {

        // 从session获取到管理员用户信息
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            // 如果admin为空，则抛出未登录异常
            return ApiRestResponse.error(RespExceptionEnum.NEED_LOGIN);
        }

        // 查询操作
        Building building = buildingService.selectById(admin, id);

        // 返回宿舍楼信息
        return ApiRestResponse.success(building);
    }
}
